/*
 * 文件名：AdInfoServiceImpl.java	 
 * 时     间：下午3:34:59
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.local.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.billRule.init.InitRule;
import com.cz073586.common.billRules.entitys.vo.ParkBillingRuleForOneDayVo;
import com.cz073586.common.billRules.manager.ParkRulesEngineManager;
import com.cz073586.tools.util.I18nUtil;
import com.local.dao.mapers.LocalCarTypeMapper;
import com.local.dao.mapers.LocalChargeBytimeMapper;
import com.local.dao.mapers.LocalWeekSectionMapper;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.model.LocalCarType;
import com.local.model.LocalChargeBytime;
import com.local.model.LocalChargeBytimeExample;
import com.local.model.LocalWeekSection;
import com.local.service.face.LocalChargeByTimeCopyService;
import com.local.service.face.ParkwellChargeBytimeService;
import com.local.util.WeekdayUtil;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.Tools;

/**
 * @ClassName: AdInfoServiceImpl
 * @Description: 这里用一句话描述这个类的作用
 * @author liyf
 * @date 2015年1月20日 下午3:34:59
 */
@Service
public class ParkwellChargeBytimeServiceImpl extends
		BaseServiceImpl<LocalChargeBytimeExample, LocalChargeBytime, String>
		implements ParkwellChargeBytimeService
{
	@Autowired
	private LocalChargeBytimeMapper chargeBytimeMapper;
	
	@Autowired
	private LocalCarTypeMapper carTypeMapper;
	
	@Autowired
	private LocalWeekSectionMapper localWeekSectionMapper;
	
	@Autowired
	private LocalChargeByTimeCopyService localChargeByTimeCopyService;
	
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	static SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
	
	@Override
	public BaseMapper<LocalChargeBytimeExample, LocalChargeBytime, String> getMapper()
	{
		return chargeBytimeMapper;
	}
	
	public boolean isWeek(Date outTime)
	{
		WeekdayUtil dateUtils = new WeekdayUtil();
		GregorianCalendar calendar = new GregorianCalendar();
		boolean ok = dateUtils.isWeekday(calendar, outTime);
		// 判断是否是工作日
		/* System.out.println("是工作日：" + ok); */
		return ok;
	}
	
	// 通过入场时间，出场时间，返回 时间集合，跨越的时段信息
	public List<LocalChargeBytime> reckonTime(LocalChargeBytime chargeBytime)
	{
		try
		{
			List<LocalChargeBytime> localChargeBytimeLs = null;
			if (!Tools.isEmpty(chargeBytime)
					&& !Tools.isEmpty(chargeBytime.getInTime()))
			{
				Date beginTime = chargeBytime.getInTime();
				Date endTime = !Tools.isEmpty(chargeBytime.getOutTime()) ? chargeBytime
						.getOutTime() : new Date(System.currentTimeMillis());
				
				long diff = (endTime.getTime() - beginTime.getTime())
						/ (86400 * 1000);
				// 判断当前车辆，停留了多少天
				/* System.out.println("当前车辆，停留" + diff + " 天"); */
				
				Date sb = null;
				Date se = null;
				if (diff == 0)
				{
					sb = beginTime;
					se = endTime;
					String ss = String.format("%d> Sb=%s, Se=%s", 1, sb, se);
					localChargeBytimeLs = new ArrayList<LocalChargeBytime>();
					LocalChargeBytime LocalChargeBytime = new LocalChargeBytime();
					LocalChargeBytime.setInTime(sb);
					LocalChargeBytime.setOutTime(se);
					LocalChargeBytime.setCarType(chargeBytime.getCarType());
					localChargeBytimeLs.add(LocalChargeBytime);
					return localChargeBytimeLs;
				} else
				{
					localChargeBytimeLs = new ArrayList<LocalChargeBytime>();
					int i = 0;
					while (i <= diff)
					{
						if (i == 0)
						{
							sb = beginTime;
							se = sdf.parse(ymd.format(sb) + " 23:59:59");
							LocalChargeBytime LocalChargeBytime = new LocalChargeBytime();
							LocalChargeBytime.setInTime(sb);
							LocalChargeBytime.setOutTime(se);
							LocalChargeBytime.setCarType(chargeBytime
									.getCarType());
							localChargeBytimeLs.add(LocalChargeBytime);
						} else if (i == diff)
						{
							sb = sdf.parse(ymd.format(endTime) + " 00:00:00");
							se = endTime;
							LocalChargeBytime LocalChargeBytime = new LocalChargeBytime();
							LocalChargeBytime.setInTime(sb);
							LocalChargeBytime.setOutTime(se);
							LocalChargeBytime.setCarType(chargeBytime
									.getCarType());
							localChargeBytimeLs.add(LocalChargeBytime);
						} else
						{
							se = new Date(beginTime.getTime() + i * 24 * 60
									* 60 * 1000);
							sb = sdf.parse(ymd.format(se) + " 00:00:00");
							se = sdf.parse(ymd.format(se) + " 23:59:59");
							LocalChargeBytime LocalChargeBytime = new LocalChargeBytime();
							LocalChargeBytime.setInTime(sb);
							LocalChargeBytime.setOutTime(se);
							LocalChargeBytime.setCarType(chargeBytime
									.getCarType());
							localChargeBytimeLs.add(LocalChargeBytime);
						}
						String ss = String
								.format("%d> Sb=%s, Se=%s", i, sb, se);
						// 停留的时间段
						/* System.out.println(ss); */
						i++;
					}
					return localChargeBytimeLs;
				}
			}
			return null;
		} catch (Exception e)
		{
			return null;
		}
	}
	
	@Override
	public double queryBilling(LocalChargeBytime chargeBytime,
			ParkwellPayBillVo parkwellPayBillVo) throws BaseException
	{
		
		try
		{
			if (!Tools.isEmpty(chargeBytime)
					&& !Tools.isEmpty(chargeBytime.getInTime())
					&& !Tools.isEmpty(chargeBytime.getOutTime()))
			{
				/* double hours = (double) Math.ceil(time / 1000 / 3600); */
				if (Tools.isEmpty(chargeBytime.getCarType()))
				{
					LocalCarType carType = new LocalCarType();
					List<LocalCarType> list = carTypeMapper
							.queryByPage(carType);
					if (Tools.isEmpty(list))
					{
						return 0;
					} else
					{
						chargeBytime.setCarType(list.get(0).getCarTypeUuid());
					}
				}
				double mm = 0;
				// 算出停了多少时间 ,多少天，时间段信息
				List<LocalChargeBytime> localChargeBytimeLs = reckonTime(chargeBytime);
				if (!Tools.isEmpty(localChargeBytimeLs))
				{
					// 便利天数集合，算出每天的钱，累加
					for (LocalChargeBytime lb : localChargeBytimeLs)
					{
						boolean isw = isWeek(lb.getOutTime());
						List<LocalChargeBytime> list = findWeekOrNoWeek(lb, isw);
						// 每一天停留的时间
						double time = lb.getOutTime().getTime()
								- lb.getInTime().getTime();
						double mins = (double) Math.ceil(time / 1000 / 60);
						mm += countPrice(lb, list, mins);
					}
				}
				return mm;
			}
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			parkwellPayBillVo.setError(true);
			parkwellPayBillVo.setErrorMsg(I18nUtil
					.getErroryCodeInfo("50000-00001-00050"));
			// throw new RuntimeException("50000-00001-00050");
		}
		return 0;
	}
	
	public List<LocalChargeBytime> findWeekOrNoWeek(
			LocalChargeBytime chargeBytime, boolean falg)
	{
		List<LocalWeekSection> lWeekSLs = localWeekSectionMapper
				.loadAllWeekSection(chargeBytime.getCarType());
		List<LocalChargeBytime> list1 = new ArrayList<LocalChargeBytime>();// 工作日规则
		List<LocalChargeBytime> list2 = new ArrayList<LocalChargeBytime>();// 非工作日规则
		List<LocalChargeBytime> list3 = new ArrayList<LocalChargeBytime>();// 不区分规则
		// 从工作列表，抽出3种规则 3:无区分规则 1:工作日规则 2:非工作日规则
		for (LocalWeekSection lws : lWeekSLs)
		{
			if (lws.getType().equals("3"))
			{
				LocalChargeBytime lct = chargeBytimeMapper
						.selectByPrimaryKey(lws.getChargeBytimeUuid());
				lct.setInTime(chargeBytime.getInTime());
				lct.setOutTime(chargeBytime.getOutTime());
				lct.setCarType(chargeBytime.getCarType());
				if (lws.getSection().equals("1"))
				{
					lct.setSection("1");
					lct.setSectionStartTime(lws.getSectionStartTime());
					lct.setSectionEndTime(lws.getSectionEndTime());
				} else
				{
					lct.setSection("2");
				}
				list3.add(lct);
			}
			if (lws.getType().equals("1"))
			{
				LocalChargeBytime lct = chargeBytimeMapper
						.selectByPrimaryKey(lws.getChargeBytimeUuid());
				lct.setInTime(chargeBytime.getInTime());
				lct.setOutTime(chargeBytime.getOutTime());
				lct.setCarType(chargeBytime.getCarType());
				if (lws.getSection().equals("1"))
				{
					lct.setSection("1");
					lct.setSectionStartTime(lws.getSectionStartTime());
					lct.setSectionEndTime(lws.getSectionEndTime());
				} else
				{
					lct.setSection("2");
				}
				list1.add(lct);
			}
			if (lws.getType().equals("2"))
			{
				LocalChargeBytime lct = chargeBytimeMapper
						.selectByPrimaryKey(lws.getChargeBytimeUuid());
				lct.setInTime(chargeBytime.getInTime());
				lct.setOutTime(chargeBytime.getOutTime());
				lct.setCarType(chargeBytime.getCarType());
				if (lws.getSection().equals("1"))
				{
					lct.setSection("1");
					lct.setSectionStartTime(lws.getSectionStartTime());
					lct.setSectionEndTime(lws.getSectionEndTime());
				} else
				{
					lct.setSection("2");
				}
				list2.add(lct);
			}
		}
		// 如果列表中，含有 3:无区分规则，那么直接返回，规则信息
		// 否则含有 1:工作日规则，那么直接返回，规则信息
		// 否则含有 1:工作日规则，那么直接返回，规则信息
		for (LocalWeekSection tt : lWeekSLs)
		{
			if (tt.getType().equals("3"))
			{
				return list3;
			} else if (tt.getType().equals("1") && falg == true)
			{
				return list1;
			} else if (tt.getType().equals("2") && falg == false)
			{
				return list2;
			}
		}
		if (list3.size() > 0)
		{
			return list3;
		} else if (falg == true)
		{
			return list1;
		} else
		{
			return list2;
		}
	}
	
	public double countPrice(LocalChargeBytime chargeBytime,
			List<LocalChargeBytime> list, double mins)
	{
		// 如果没有计费规则。返回0元
		if (Tools.isEmpty(list.size() == 0))
		{
			return 0;
		} else
		{
			// 如果有计费规则，可分为：分段收费
			LocalChargeBytime liSection = new LocalChargeBytime();
			// 如果有计费规则，无分段收费
			LocalChargeBytime liNoSection = new LocalChargeBytime();
			// 如果规则条数 大于1，则有分段收费
			if (list.size() > 1)
			{
				for (LocalChargeBytime lc : list)
				{
					if (lc.getSection().equals("1"))
					{
						liSection = lc;
					} else
					{
						liNoSection = lc;
					}
				}
			} else
			{
				// 如果规则<2,就这条规则
				chargeBytime = list.get(0);
				if (chargeBytime.getSection().equals("1"))
				{
					liSection = chargeBytime;
					return sectionPrice(liSection, mins, false);
				} else
				{
					liNoSection = chargeBytime;
					return noSectionPrice(liNoSection, mins, false);
				}
			}
			return seNosePrice(liSection, liNoSection, mins);
		}
	}
	
	public double seNosePrice(LocalChargeBytime liSection,
			LocalChargeBytime liNoSection, double mins)
	{
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			boolean star = isInDate(liSection.getInTime(),
					liSection.getSectionStartTime(),
					liSection.getSectionEndTime());
			boolean end = isInDate(liSection.getOutTime(),
					liSection.getSectionStartTime(),
					liSection.getSectionEndTime());
			if (star == true && end == true)
			{
				return sectionPrice(liSection, mins, false);
			} else if (star == true && end == false)
			{
				// 分段结束时间
				Long se = sdf.parse(liSection.getSectionEndTime()).getTime();// 11结束
				// 车辆出去时间
				Long carOu = sdf.parse(sdf.format(liSection.getOutTime()))
						.getTime();// 14chuqu
				// 停了多长时间3
				Long later = carOu - se;
				double minslater = (double) Math.ceil(later / 1000 / 60);
				mins = mins - minslater;
				return sectionPrice(liSection, mins, false)
						+ noSectionPrice(liNoSection, minslater, true);
			} else if (star == false && end == true)
			{
				// 分段开始时间
				Long se = sdf.parse(liSection.getSectionStartTime()).getTime();
				// 车辆进去时间
				Long carIn = sdf.parse(sdf.format(liSection.getInTime()))
						.getTime();
				// 停了多长时间
				Long erey = se - carIn;
				double minsErey = (double) Math.ceil(erey / 1000 / 60);
				mins = mins - minsErey;
				return sectionPrice(liSection, mins, true)
						+ noSectionPrice(liNoSection, minsErey, false);
			} else if (star == false && end == false)
			{
				return noSectionPrice(liNoSection, mins, false);
			} else
			{
				return 0;
			}
		} catch (Exception e)
		{
			// TODO: handle exception
		}
		return 0;
		
	}
	
	/**
	 * 
	 * 方法描述：判断时间在时间段
	 * 
	 * @Title: isInDate
	 * @param @param date
	 * @param @param strDateBegin
	 * @param @param strDateEnd
	 * @param @return
	 * @return boolean
	 * @user Administrator 2015年10月7日
	 * @throws
	 */
	public static boolean isInDate(Date date, String strDateBegin,
			String strDateEnd)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strDate = sdf.format(date);
		// 截取当前时间时分秒
		int strDateH = Integer.parseInt(strDate.substring(11, 13));
		int strDateM = Integer.parseInt(strDate.substring(14, 16));
		int strDateS = Integer.parseInt(strDate.substring(17, 19));
		// 截取开始时间时分秒
		int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));
		int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));
		int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));
		// 截取结束时间时分秒
		int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));
		int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));
		int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));
		if ((strDateH >= strDateBeginH && strDateH <= strDateEndH))
		{
			// 当前时间小时数在开始时间和结束时间小时数之间
			if (strDateH > strDateBeginH && strDateH < strDateEndH)
			{
				return true;
				// 当前时间小时数等于开始时间小时数，分钟数在开始和结束之间
			} else if (strDateH == strDateBeginH && strDateM >= strDateBeginM
					&& strDateM <= strDateEndM)
			{
				return true;
				// 当前时间小时数等于开始时间小时数，分钟数等于开始时间分钟数，秒数在开始和结束之间
			} else if (strDateH == strDateBeginH && strDateM == strDateBeginM
					&& strDateS >= strDateBeginS && strDateS <= strDateEndS)
			{
				return true;
			}
			// 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数小等于结束时间分钟数
			else if (strDateH >= strDateBeginH && strDateH == strDateEndH
					&& strDateM <= strDateEndM)
			{
				return true;
				// 当前时间小时数大等于开始时间小时数，等于结束时间小时数，分钟数等于结束时间分钟数，秒数小等于结束时间秒数
			} else if (strDateH >= strDateBeginH && strDateH == strDateEndH
					&& strDateM == strDateEndM && strDateS <= strDateEndS)
			{
				return true;
			} else
			{
				return false;
			}
		} else
		{
			return false;
		}
	}
	
	public double sectionPrice(LocalChargeBytime chargeBytime, double mins,
			boolean falg)
	{
		if (Tools.isEmpty(chargeBytime))
		{
			return 0;
		} else
		{
			
			double acMins = 0;
			if (falg == true)
			{
				acMins = mins;
			} else
			{
				acMins = mins
						- (Double.parseDouble(chargeBytime.getFreeTime()) * 60);
			}
			// 停车时间-免费时间 =实际停车时间
			
			if (acMins >= Double.parseDouble(chargeBytime.getAccountTime()) * 60)
			{
				return Double.parseDouble(!Tools.isEmpty(chargeBytime
						.getMaxCharge()) ? chargeBytime.getMaxCharge() : "0");
			} else
			{
				// min :停车时间 1.停车时间<免费时间，按免费时间收费
				if (mins < Double.parseDouble(chargeBytime.getFreeTime()) * 60)
				{
					return Double.parseDouble(chargeBytime.getFreeTimeCharge());
				}
				// 2.如果停车时间大于免费时间
				// 实际停车时间<起始时间= 起始支付
				else if (acMins < Double
						.parseDouble(chargeBytime.getBaseTime()) * 60)
				{
					return Double.parseDouble(chargeBytime.getFreeTimeCharge())
							+ Double.parseDouble(chargeBytime
									.getBaseTimeCharge());
				}
				// 2.停车时间超出单位时间，按单位时间收费
				else
				{
					acMins = acMins
							- Double.parseDouble(chargeBytime.getBaseTime())
							* 60;
					double accountHour = (double) Math
							.ceil(acMins
									/ (Double.parseDouble(chargeBytime
											.getSupassTime()) * 60));
					
					BigDecimal freePrice = new BigDecimal(
							!Tools.isEmpty(chargeBytime.getFreeTimeCharge()) ? chargeBytime
									.getFreeTimeCharge() : null);
					BigDecimal basePrice = new BigDecimal(
							!Tools.isEmpty(chargeBytime.getBaseTimeCharge()) ? chargeBytime
									.getBaseTimeCharge() : null);
					BigDecimal AccountPrice = new BigDecimal(accountHour
							* Double.parseDouble(chargeBytime
									.getAccountTimeCharge()));
					
					double money = freePrice.add(basePrice).add(AccountPrice)
							.doubleValue();
					
					if (!Tools.isEmpty(chargeBytime.getMaxCharge()))
					{
						if (money > Double.parseDouble(chargeBytime
								.getMaxCharge()))
						{
							return Double.parseDouble(chargeBytime
									.getMaxCharge());
						}
					}
					return money;
				}
			}
		}
	}
	
	public double noSectionPrice(LocalChargeBytime chargeBytime, double mins,
			boolean falg)
	{
		try
		{
			if (Tools.isEmpty(chargeBytime))
			{
				return 0;
			}
			// 停车时间-免费时间 =实际停车时间 //当存在分段收费时，免费时间已经算过
			double acMins = 0;
			if (falg == true)
			{
				acMins = mins;
			} else
			{
				acMins = mins
						- (Double.parseDouble(chargeBytime.getFreeTime()) * 60);
			}
			
			if (acMins >= Double.parseDouble(chargeBytime.getAccountTime()) * 60)
			{
				return Double.parseDouble(!Tools.isEmpty(chargeBytime
						.getMaxCharge()) ? chargeBytime.getMaxCharge() : "0");
			}
			// min :停车时间 1.停车时间<免费时间，按免费时间收费
			if (mins < Double.parseDouble(chargeBytime.getFreeTime()) * 60)
			{
				return Double.parseDouble(chargeBytime.getFreeTimeCharge());
			}
			// 2.如果停车时间大于免费时间
			// 实际停车时间<起始时间= 起始支付
			else if (acMins < Double.parseDouble(chargeBytime.getBaseTime()) * 60)
			{
				return Double.parseDouble(chargeBytime.getFreeTimeCharge())
						+ Double.parseDouble(chargeBytime.getBaseTimeCharge());
			}
			// 2.停车时间超出单位时间，按单位时间收费
			else
			{
				acMins = acMins
						- Double.parseDouble(chargeBytime.getBaseTime()) * 60;
				double accountHour = (double) Math
						.ceil(acMins
								/ (Double.parseDouble(chargeBytime
										.getSupassTime()) * 60));
				
				BigDecimal freePrice = new BigDecimal(
						!Tools.isEmpty(chargeBytime.getFreeTimeCharge()) ? chargeBytime
								.getFreeTimeCharge() : null);
				BigDecimal basePrice = new BigDecimal(
						!Tools.isEmpty(chargeBytime.getBaseTimeCharge()) ? chargeBytime
								.getBaseTimeCharge() : null);
				BigDecimal AccountPrice = new BigDecimal(accountHour
						* Double.parseDouble(chargeBytime
								.getAccountTimeCharge()));
				
				double money = freePrice.add(basePrice).add(AccountPrice)
						.doubleValue();
				
				if (!Tools.isEmpty(chargeBytime.getMaxCharge()))
				{
					if (money > Double.parseDouble(chargeBytime.getMaxCharge()))
					{
						return Double.parseDouble(chargeBytime.getMaxCharge());
					}
				}
				return money;
			}
		} catch (Exception e)
		{
			return 0;
		}
	}
	
	@Override
	public Long queryFreeSec(String chargeBytimeUuids, double Price, Long StartTime)
			throws BaseException
	{
		// TODO Auto-generated method stub
		try
		{
			SimpleDateFormat ymd = new SimpleDateFormat("yyyy-MM-dd");
			LocalChargeBytime chargeBytime = new LocalChargeBytime();
			Date dd = new Date(StartTime);
			// 计算车辆，，，入场的时间
			/* System.out.println(dd.getTime()); */
			chargeBytime.setInTime(dd);
			
			// 从入场计算到---当前时间.
			chargeBytime.setOutTime(new Date());
			
			boolean flag = true;
			// 计算当前时间的 价格
			ParkBillingRuleForOneDayVo pruleBill=this.queryParkBillingRule(chargeBytimeUuids);
			double tt=localChargeByTimeCopyService.queryRuleForOneDay(pruleBill, chargeBytime.getInTime(),
					chargeBytime.getOutTime());
			BigDecimal priceInit = new BigDecimal(Price);
			BigDecimal price = new BigDecimal(Price);
			BigDecimal trt = new BigDecimal(tt);
			// 计算出差价
			Price = price.subtract(trt).setScale(2, BigDecimal.ROUND_HALF_UP)
					.doubleValue();
			chargeBytime
					.setInTime(new Date(chargeBytime.getOutTime().getTime()));
			chargeBytime.setOutTime(new Date(chargeBytime.getOutTime()
					.getTime() + (1 * 1000)));
			
			// 剩余价格，用于计算还能停车时长
			long jishu = 1*1000;//秒
			long baseTimeLong = chargeBytime.getOutTime().getTime()
					+ jishu; // 基准点第一次加一秒
			Date tmpCarInTime = dd; // 穷举进场时间
			Date tmpCarOutTime = new Date(baseTimeLong);
			
			Date endFinal  = sdf.parse(ymd.format(new Date()) + " 23:59:59");
			while (flag)
			{
				//加一秒后消费金额
				double money = localChargeByTimeCopyService.queryRuleForOneDay(pruleBill, tmpCarInTime,tmpCarOutTime);
				if(money>priceInit.doubleValue())
				{
					flag=false;
					break;
				}
				//是否是背书
				BigDecimal   b   =   new   BigDecimal(priceInit.doubleValue()/money);  
				double f1   =   b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
				if(f1==1){
					f1=1.1;
				}
				jishu = (long)(f1*jishu);  //基数翻倍
				baseTimeLong = chargeBytime.getOutTime().getTime()+jishu;
				tmpCarOutTime = new Date(baseTimeLong);
			}
			if(tmpCarOutTime.getTime()>endFinal.getTime()){
				return endFinal.getTime()-new Date(System.currentTimeMillis()).getTime();
			}else{
				return tmpCarOutTime.getTime()-new Date(System.currentTimeMillis()).getTime();
			}
		} catch (Exception e)
		{
			return null;
		}
	}
	
	@Override
	public double queryCarinBillMoney(String chargeBytimeUuids,Date carInTime, Date carOutTime)
	{
		// TODO Auto-generated method stub
		try
		{
			long startTime = System.currentTimeMillis();
			List<LocalChargeBytime> list = this.queryLsCarInUuid(chargeBytimeUuids);
			log.debug("查询计费副本耗时queryLsCarInUuid:"+(System.currentTimeMillis()-startTime)+"毫秒");
			startTime = System.currentTimeMillis();
			ParkBillingRuleForOneDayVo billVo = InitRule.parserParkBillRule(list);
			log.debug("计算费用耗时parserParkBillRule:"+(System.currentTimeMillis()-startTime)+"毫秒");
			billVo.setOriginalCarInTime(carInTime);
			billVo.setOriginalCarOutTime(carOutTime);
			ParkRulesEngineManager.instance().startBill(billVo);
			return billVo.getAllBill();
		} catch (Exception e)
		{
			log.error("--------1---------", e);
			throw new BaseException("--------1---------");
		}
	}
	
	
	@Override
	public List<LocalChargeBytime> queryLsCarInUuid(String chargeBytimeUuids)
	{
		try
		{
			long startTime = System.currentTimeMillis();
			long begainTime = startTime;
			String[] uuids = chargeBytimeUuids.split(",");
			List<LocalChargeBytime> list= new ArrayList<LocalChargeBytime>();
			for (String uuid : uuids) {
				LocalChargeBytime chargeBytime = chargeBytimeMapper.queryLsByCarInUuid(uuid);
				if(!Tools.isEmpty(chargeBytime)){
					list.add(chargeBytime);
				}
			}
			log.debug("->查询入场计费副本:"+(System.currentTimeMillis()-startTime)+"毫秒");
			/*if(!Tools.isEmpty(list)&&list.size()>0){
				for(int i=0;i<list.size();i++){
					LocalChargeBytime lb=list.get(i);
				    startTime = System.currentTimeMillis();
					lb.setCartypeName(carTypeMapper.selectCarNameByUuid(lb.getCarType()).getCartypeName());
					log.debug("->车辆类型:"+(System.currentTimeMillis()-startTime)+"毫秒");
					startTime = System.currentTimeMillis();
					LocalWeekSectionCopy lws=localWeekSectionCopyService.loadAllWeekSectionCopyByChargUuid(lb.getChargeBytimeUuid());
					log.debug("loadAllWeekSectionCopyByChargUuid:"+(System.currentTimeMillis()-startTime)+"毫秒");
					lb.setType(lws.getType());
					lb.setSection(lws.getSection());
					lb.setSectionStartTime(lws.getSectionStartTime());
					lb.setSectionEndTime(lws.getSectionEndTime());
				}
			}*/
			log.debug("->查询入场计费副本,一共耗时:"+(System.currentTimeMillis()-begainTime)+"毫秒");
			return list;
		} catch (Exception e)
		{
			log.error("-------取计费规则------", e);
			throw new BaseException("-------取计费规则------");
		}
	}
	
	private ParkBillingRuleForOneDayVo queryParkBillingRule(String chargeBytimeUuids)
	{
		// TODO Auto-generated method stub
		ParkBillingRuleForOneDayVo billVo =null;
		try
		{
			List<LocalChargeBytime> list = this.queryLsCarInUuid(chargeBytimeUuids);
			billVo = InitRule.parserParkBillRule(list);
			return billVo;
		} catch (Exception e)
		{
			// TODO: handle exception
			return null;
		}
	}
}
